package com.zbkj.client.controller;

import com.zbkj.client.config.ClientTokenComponent;
import com.zbkj.common.model.client.discovery.Discovery;
import com.zbkj.common.response.client.DiscoveryListResponse;
import com.zbkj.common.result.CommonResult;
import com.zbkj.service.service.DiscoveryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import com.baomidou.mybatisplus.core.metadata.IPage;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/api/client/discovery")
@Api(tags = "发现页面")
public class DiscoveryController {
    @Autowired
    private DiscoveryService discoveryService;

    @Autowired
    private ClientTokenComponent clientTokenComponent;

    @ApiOperation(value = "获取发现列表")
    @GetMapping("/getList")
    public CommonResult<IPage<DiscoveryListResponse>> getList(@RequestParam(value = "page", defaultValue = "1") int page,
                                                                @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                                                HttpServletRequest httpRequest) {

        Integer userId = clientTokenComponent.getLoginUser(httpRequest);

        IPage<DiscoveryListResponse> result = discoveryService.getDiscoveryPageWithContacts(page, pageSize, userId);

        return CommonResult.success(result);
    }

    @ApiOperation(value = "获取发现详情")
    @GetMapping("/getDetail")
    public CommonResult<DiscoveryListResponse> getDetail(@RequestParam("id") Integer id, HttpServletRequest httpRequest) {
        Integer userId = clientTokenComponent.getLoginUser(httpRequest);

        DiscoveryListResponse discovery = discoveryService.getDetailWithContacts(id, userId);
        if (discovery == null || discovery.getIsDel() != null && discovery.getIsDel() == 1) {
            return CommonResult.failed("未找到该发现记录");
        }
        return CommonResult.success(discovery);
    }
} 